package com.qiaofang.service.impl;

import com.qiaofang.common.constant.ExceptionEnum;
import com.qiaofang.common.exception.BusinessException;
import com.qiaofang.common.model.PageDTO;
import com.qiaofang.common.model.UiListBean;
import com.qiaofang.dto.PartnerPremisesDTO;
import com.qiaofang.dto.SearchWxMsgFailureDTO;
import com.qiaofang.dto.WxMsgFailureDTO;
import com.qiaofang.entity.PartnerInfo;
import com.qiaofang.entity.WxMsgFailure;
import com.qiaofang.entity.enums.SendStatusEnum;
import com.qiaofang.entity.enums.SystemEnum;
import com.qiaofang.entity.enums.UserRoleEnum;
import com.qiaofang.entity.enums.WxMsgStatusEnum;
import com.qiaofang.mapper.WxMsgFailureMapper;
import com.qiaofang.repository.CompanyRepository;
import com.qiaofang.repository.PartnerInfoRepository;
import com.qiaofang.repository.PremisesRepository;
import com.qiaofang.repository.WxMsgFailureRepository;
import com.qiaofang.service.WxMsgFailureService;
import com.qiaofang.vo.WxMsgFailureVO;
import com.qiaofang.vo.WxMsgStatusVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author qi.liu
 * @date: 2021/1/12 11:13
 */
@Service
public class WxMsgFailureServiceImpl implements WxMsgFailureService {

    @Autowired
    private WxMsgFailureRepository wxMsgFailureRepository;

    @Autowired
    private WxMsgFailureMapper wxMsgFailureMapper;

    @Autowired
    private PartnerInfoRepository partnerInfoRepository;

    @Autowired
    private PremisesRepository premisesRepository;

    @Autowired
    private CompanyRepository companyRepository;

    @Override
    public void updateWxMsgFailure(WxMsgFailureDTO wxMsgFailureDTO) {
        WxMsgFailure wxMsgFailure;
        if (wxMsgFailureDTO.getId() != null) {
            wxMsgFailure = wxMsgFailureRepository.findById(wxMsgFailureDTO.getId())
                    .orElseThrow(() -> new BusinessException(ExceptionEnum.NOT_FIND_DB_DATA));


            wxMsgFailure.setReceiveOpenid(wxMsgFailureDTO.getReceiveOpenid());
            wxMsgFailure.setReceiveContent(wxMsgFailureDTO.getReceiveContent());

//            wxMsgFailure.setCompanyUuid(wxMsgFailureDTO.getCompanyUuid());
//            wxMsgFailure.setSystemId(wxMsgFailureDTO.getSystemId());
//            wxMsgFailure.setPartnerId(wxMsgFailureDTO.getPartnerId());
//            wxMsgFailure.setCusPreId(wxMsgFailureDTO.getCusPreId());
//            wxMsgFailure.setWxModelType(wxMsgFailureDTO.getWxModelType());
//            wxMsgFailure.setWxModelUuid(wxMsgFailureDTO.getWxModelUuid());
//            wxMsgFailure.setSendStatus(wxMsgFailureDTO.getSendStatus());
//            wxMsgFailure.setResult(wxMsgFailureDTO.getResult());
//            wxMsgFailure.setRepeatResult(wxMsgFailureDTO.getRepeatResult());

            wxMsgFailureRepository.save(wxMsgFailure);
        }
    }

    @Override
    public UiListBean listWxMsgFailure(SearchWxMsgFailureDTO dto, PageDTO page) {
        UiListBean<WxMsgFailureVO> uiListBean = new UiListBean();

        if (StringUtils.isNotBlank(dto.getCompanyUuid())){
            dto.setCompanyUuid(companyRepository.findCompanyUuid(dto.getCompanyUuid()));
        }

        List<WxMsgFailure> WxMsgFailureList = wxMsgFailureMapper.listWxMsgFailure(dto, page);
        List<WxMsgFailureVO> voList = new ArrayList<>();
        if (null == WxMsgFailureList || WxMsgFailureList.size() == 0) {
            return uiListBean;
        }
        for (WxMsgFailure wxMsgFailure : WxMsgFailureList) {
            WxMsgFailureVO vo = new WxMsgFailureVO();
            BeanUtils.copyProperties(wxMsgFailure, vo);
            if (null!=vo.getPartnerId()){
                vo.setPartnerName(partnerInfoRepository.findNameById(vo.getPartnerId()));
            }
            if (StringUtils.isNotBlank(vo.getCompanyUuid())){
                vo.setCompanyUuid(companyRepository.findCompanyName(vo.getCompanyUuid()));
            }
            if (StringUtils.isNotBlank(vo.getSystemId())) {
                vo.setSystemId(SystemEnum.getDescByNo(vo.getSystemId()));
            }
            if (StringUtils.isNotBlank(vo.getSendStatus())) {
                vo.setSendStatus(SendStatusEnum.getByNo(vo.getSendStatus()).getDes());
            }
            voList.add(vo);
        }
        uiListBean.setList(voList);
        uiListBean.setTotalCount(wxMsgFailureMapper.listWxMsgFailureCount(dto));
        return uiListBean;
    }

    @Override
    public WxMsgStatusVO getWxMsgStatus(PartnerPremisesDTO dto) {
        PartnerInfo partnerInfo = partnerInfoRepository.findById(dto.getPartnerId())
                .orElseThrow(() -> new BusinessException(ExceptionEnum.NOT_FIND_DB_DATA));
        premisesRepository.findById(dto.getPremisesId())
                .orElseThrow(() -> new BusinessException(ExceptionEnum.NOT_FIND_DB_DATA));
        WxMsgStatusVO vo = new WxMsgStatusVO();
        List<Integer> list = wxMsgFailureMapper.findPartnerId(dto.getPremisesId());
        if (null == list || list.size() == 0) {
            vo.setPartnerPremises("本楼盘没有驻场人员");
        }else {
            vo.setPartnerPremises("不是本楼盘的驻场人员");
            for (Integer p : list) {
                if (dto.getPartnerId()==p.longValue()){
                    vo.setPartnerPremises("是本楼盘的驻场人员");
                }
            }
        }
        vo.setStatus(wxMsgFailureMapper.findWxMsgStatus(dto));
        vo.setUserRole(partnerInfo.getUserRole());
        if (StringUtils.isNotBlank(vo.getStatus())) {
            vo.setStatus(WxMsgStatusEnum.getDescByNo(vo.getStatus()));
        }
        if (StringUtils.isNotBlank(vo.getUserRole())) {
            vo.setUserRole(UserRoleEnum.getDescByNo(vo.getUserRole()));
        }
        return vo;
    }
}
